package com.client.koshercook.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RecipesTable extends Composite {

	private HorizontalPanel Hpanel = new HorizontalPanel();
	private VerticalPanel Vpanel = new VerticalPanel();
	private VerticalPanel PicVpanel = new VerticalPanel();
	private TabPanel tabPanel = new TabPanel(); // the main tab panel
	private VerticalPanel recsPanel = new VerticalPanel();
	private FlexTable recipesTable = new FlexTable();
	private ScrollPanel scrollerRecs = new ScrollPanel(recipesTable);
	final Image chef = new Image("chef_help_bold.gif");
	final Image helpP = new Image("help.png");
	private Explanation explanation = new Explanation();
	private NextBack nextBack;
	
	/** constructor for creating a new empty recipesTable object */
	public RecipesTable(){
		
		// creating the different tables
		initRecipeTable(recipesTable);
		
		//making the tables scroll-able
		scrollerRecs.setSize("580px", "400px");
		recsPanel.add(scrollerRecs);
		
		tabPanel.add(recsPanel, "תוצאות החיפוש");
		tabPanel.selectTab(0);
		
		chef.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				explanation.show();
				helpP.setVisible(false);
			}
		});
		
		chef.addMouseOverHandler(new MouseOverHandler(){
			public void onMouseOver(MouseOverEvent event) {
				helpP.setVisible(true);
			}
		});
		
		chef.addMouseOutHandler(new MouseOutHandler(){
			public void onMouseOut(MouseOutEvent event) {
				helpP.setVisible(false);
			}
		});
		
		helpP.setVisible(false);
		Vpanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		Vpanel.add(tabPanel);
		nextBack = new NextBack();
		Vpanel.add(nextBack);
		PicVpanel.add(helpP);
		PicVpanel.add(chef);
		Hpanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
		Hpanel.add(PicVpanel);
		Hpanel.add(Vpanel);
		
		initWidget(Hpanel);
	}

	
	/** update the table with new data from the server */
	public void update(){
		fillRecipeTable();
	}

	
	/** clear the recipes table */
	public void clear() {
		while (recipesTable.getRowCount() > 1)
			recipesTable.removeRow(1);
	}

	
	/** fills the recipe table with data from server. */
	private void fillRecipeTable() {

		ClientResponse response = Client.model.response;	//this is only for shorter code
		
		if (response == null)
			return;

		// clearing previous recipes
		clear();

		//no recipes then return
		if (response.size()==0){
			Client.model.quickPopup("לא נמצאו תוצאות לחיפוש המבוקש");
			nextBack.hide();
			return;
		}
		
		//set pageText visible to be true
		nextBack.setPageTextVisible(true);
		
		//set Next & Back buttons visibility
		//if last page, no need for Next button
		if( ( Client.model.response.size() < Model.PAGE_SIZE * 4 )	
			&& ( ((Client.model.request.getPage() % 4) + 1) *  Model.PAGE_SIZE > response.size() ) )
			nextBack.setNextButtonVisible(false);
		else
			nextBack.setNextButtonVisible(true);

		//if first page, no need for Back button
		if(Client.model.request.getPage()==0)
			nextBack.setBackButtonVisible(false);
		else
			nextBack.setBackButtonVisible(true);
		
		// inserting the data to the table
		int startIndx = (Client.model.request.getPage() % 4) * Model.PAGE_SIZE;
		int endIndx = min(startIndx + Model.PAGE_SIZE, response.size());
		int ind=1;
		for (int i = startIndx; i < endIndx; i++) {
			recipesTable.getRowFormatter().addStyleName(ind, "IngListColums");
			recipesTable.setWidget(ind, 4, new RecipeButton(response.getLink(i), response.getName(i)));
			recipesTable.setText(ind, 3, response.getKosher(i));
			recipesTable.setText(ind, 2, response.getType(i));
			if(response.getAlsoNeed(i).equals("!יש הכל"))
				recipesTable.setText(ind, 1, "!יש הכל");
			else
				recipesTable.setWidget(ind, 1, newAlsoNeedDisclosure(response.getAlsoNeed(i)));
			recipesTable.setWidget(ind, 0, new ImageLink(response.getLink(i), response.getName(i)));
			ind++;
		}
	}
	
	
	/** init the recipes tables */
	private void initRecipeTable(FlexTable recipesTable) {
		recipesTable.setText(0, 4, "שם המתכון");
		recipesTable.setText(0, 3, "כשרות");
		recipesTable.setText(0, 2, "סוג");
		recipesTable.setText(0, 1, "רכיבים חסרים");
		recipesTable.setText(0, 0, "קישור חיצוני");
		for(int i=0; i<5; i++)
			recipesTable.getCellFormatter().addStyleName(0, i, "IngListColumsHeader");
		recipesTable.setBorderWidth(3);
		recipesTable.addStyleName("recipesTable");
	}

	
	/** create a new disclosure panel with the also need data inside it */
	private DisclosurePanel newAlsoNeedDisclosure(String alsoNeed) {
		final DisclosurePanel dp = new DisclosurePanel("לחץ לפירוט");
		dp.setContent(new HTML(alsoNeed));
		return dp;
	}
	
	
	private int min(int x, int y) {
		if(x < y)
			return x;
		return y;
	}
	
	
	public void setSmaller(){
		scrollerRecs.setSize("450px", "200px");
		PicVpanel.remove(chef);
	}
	
	
	public void hideNextBack(){
		nextBack.hide();
	}
}